package linkeddataweb.sparql;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import linkeddataweb.professor.Professor;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.sparql.core.ResultBinding;
import com.hp.hpl.jena.tdb.TDBFactory;

public class Sparql {
	private String dir;
	private String search;
	private String type;
	private String name;
	private String[] language = new String[2];
	private String[] fullPapers = new String[2];
	private String[] attendedEvents = new String[2];
	private String[] knows = new String[2];
	private Professor prof;
	Model model;
	private ArrayList<Professor> arrayProf = new ArrayList<Professor>();

	public Sparql(String dir, String search, String type) {
		this.dir = dir;
		this.search = search;
		this.type = type;
		model = TDBFactory.createModel(dir);
		if(type.equals("query")){
			queryType(true);
		}else queryType(false);
		model.close();
	}
	
	public Sparql(String dir, String name) {
		this.dir = dir;
		this.name = name;
		model = TDBFactory.createModel(dir);
		//queryName(name);
		model.close();
	}

	public ArrayList<Professor> getArrayProf() {
		return arrayProf;
	}

	public void setProf(ArrayList<Professor> arrayProf) {
		this.arrayProf = arrayProf;
	}

	private void queryLanguage(Professor prof, String id) {
		String query = "SELECT  ?name ?lingua "
				+ "WHERE { ?x <http://xmlns.com/foaf/0.1/name> ?name. "
				+ "?x <http://purl.org/dc/elements/1.1/language> ?lingua. "
				+ "FILTER regex(?name,\'" + id + "\','i') }";

		Query queryFact = QueryFactory.create(query);
		QueryExecution qexec = QueryExecutionFactory.create(queryFact, model);
		ResultSet results = qexec.execSelect();
		int i = 0;
		for (Iterator iter = results; iter.hasNext();) {
			ResultBinding res = (ResultBinding) iter.next();
			language[i] = res.get("lingua").toString();
			i++;
		}

		prof.setLanguages(language);
	}

	private void queryPapers(Professor prof, String id) {
		String query = "SELECT  ?name ?pub "
				+ "WHERE { ?x <http://xmlns.com/foaf/0.1/name> ?name. "
				+ "?x <http://xmlns.com/foaf/0.1/publications> ?pub. "
				+ "FILTER regex(?name,\'" + id + "\','i') }";

		Query queryFact = QueryFactory.create(query);
		QueryExecution qexec = QueryExecutionFactory.create(queryFact, model);
		ResultSet results = qexec.execSelect();
		int i = 0;
		for (Iterator iter = results; iter.hasNext();) {
			ResultBinding res = (ResultBinding) iter.next();
			fullPapers[i] = res.get("pub").toString();
			i++;
		}
		prof.setFullPapers(fullPapers);
	}

	private void queryEvent(Professor prof, String id) {
		String query = "SELECT  ?name ?evento "
				+ "WHERE { ?x <http://xmlns.com/foaf/0.1/name> ?name. "
				+ "?x <http://www.w3.org/2001/vcard-rdf/3.0#Other> ?evento. "
				+ "FILTER regex(?name,\'" + id + "\','i') }";

		Query queryFact = QueryFactory.create(query);
		QueryExecution qexec = QueryExecutionFactory.create(queryFact, model);
		ResultSet results = qexec.execSelect();
		int i = 0;
		for (Iterator iter = results; iter.hasNext();) {
			ResultBinding res = (ResultBinding) iter.next();
			attendedEvents[i] = res.get("evento").toString();
			i++;
		}
		prof.setAttendedEvents(attendedEvents);
	}

	private void queryKnows(Professor prof, String id) {
		String query = "SELECT  ?name ?knows "
				+ "WHERE { ?x <http://xmlns.com/foaf/0.1/name> ?name. "
				+ "?x <http://xmlns.com/foaf/0.1/knows> ?knows. "
				+ "FILTER regex(?name,\'" + id + "\','i') }";

		Query queryFact = QueryFactory.create(query);
		QueryExecution qexec = QueryExecutionFactory.create(queryFact, model);
		ResultSet results = qexec.execSelect();
		int i = 0;
		for (Iterator iter = results; iter.hasNext();) {
			ResultBinding res = (ResultBinding) iter.next();
			knows[i] = res.get("knows").toString();
			i++;
		}
		prof.setKnows(knows);
	}

	private void queryAll(Professor prof, String id) {
		String query = "SELECT  ?name ?nick ?end ?email ?univ ?lattes "
				+ "?page ?foto ?title ?topic_interest ?proj ?proj_ant "
				+ "WHERE { ?x <http://xmlns.com/foaf/0.1/name> ?name. "
				+ "?x <http://xmlns.com/foaf/0.1/nick> ?nick. "
				+ "?x <http://www.w3.org/2001/vcard-rdf/3.0#ADR> ?end. "
				+ "?x <http://www.w3.org/2001/vcard-rdf/3.0#EMAIL> ?email. "
				+ "?x <http://www.w3.org/2001/vcard-rdf/3.0#ORG> ?univ. "
				+ "?x <http://xmlns.com/foaf/0.1/workInfoHomepage> ?lattes. "
				+ "?x <http://xmlns.com/foaf/0.1/homepage> ?page. "
				+ "?x <http://www.w3.org/2001/vcard-rdf/3.0#PHOTO> ?foto. "
				+ "?x <http://xmlns.com/foaf/0.1/title> ?title. "
				+ "?x <http://xmlns.com/foaf/0.1/topic_interest> ?topic_interest. "
				+ "?x <http://xmlns.com/foaf/0.1/currentProject> ?proj. "
				+ "?x <http://xmlns.com/foaf/0.1/pastProject> ?proj_ant. "
				+ "FILTER regex(?name,\'" + id + "\','i') }";

		Query queryFact = QueryFactory.create(query);
		QueryExecution qexec = QueryExecutionFactory.create(queryFact, model);
		ResultSet results = qexec.execSelect();

		ResultBinding r = (ResultBinding) results.next();
		prof.setName(r.get("name").toString());
		prof.setCitationName(r.get("nick").toString());
		prof.setAddress(r.get("end").toString());
		prof.setEmail(r.get("email").toString());
		prof.setUniversityName(r.get("univ").toString());
		prof.setLattes(r.get("lattes").toString());
		prof.setWebpage(r.get("page").toString());
		prof.setImage(r.get("foto").toString());
		prof.setHigherDegree(r.get("title").toString());
		prof.setProfissionalArea(r.get("topic_interest").toString());
		prof.setProject(r.get("proj").toString());
		prof.setPastProject(r.get("proj_ant").toString());
		qexec.close();
	}
	
	private void queryType(boolean b){
		String query = null;
		if(b){
			query = search;
		}else{
			query = "SELECT  ?name ?title ?topic_interest "
				+ "WHERE { ?x <http://xmlns.com/foaf/0.1/name> ?name. "
				+ "?x <http://xmlns.com/foaf/0.1/title> ?title. "
				+ "?x <http://xmlns.com/foaf/0.1/topic_interest> ?topic_interest. "
				+ "FILTER regex(?" + type + ",\'" + search + "\','i') }";
		}
		Query queryFact = QueryFactory.create(query);
		QueryExecution qexec = QueryExecutionFactory.create(queryFact, model);
		ResultSet results = qexec.execSelect();

		for (Iterator iter = results; iter.hasNext();) {
			ResultBinding res = (ResultBinding) iter.next();
			Object id = res.get("name");
			prof = new Professor();
			queryAll(prof,id.toString());
			queryLanguage(prof,id.toString());
			queryEvent(prof,id.toString());
			queryPapers(prof,id.toString());
			queryKnows(prof,id.toString());
			arrayProf.add(prof);
		}
	}

	public Professor queryName(String name) {
		String query = "SELECT  ?name ?title ?topic_interest "
				+ "WHERE { ?x <http://xmlns.com/foaf/0.1/name> ?name. "
				+ "?x <http://xmlns.com/foaf/0.1/title> ?title. "
				+ "?x <http://xmlns.com/foaf/0.1/topic_interest> ?topic_interest. "
				+ "FILTER regex(?name,\'" + name + "\','i') }";
		Query queryFact = QueryFactory.create(query);
		QueryExecution qexec = QueryExecutionFactory.create(queryFact, model);
		ResultSet results = qexec.execSelect();

		for (Iterator iter = results; iter.hasNext();) {
			ResultBinding res = (ResultBinding) iter.next();
			Object id = res.get("name");
			prof = new Professor();
			queryAll(prof, id.toString());
			queryLanguage(prof, id.toString());
			queryEvent(prof, id.toString());
			queryPapers(prof, id.toString());
			queryKnows(prof, id.toString());
			//arrayProf.add(prof);
		}
		return prof;
	}
}
